﻿using FSM.IBll;
using FSM.Model;
using FSM.Web.Models;
using FSM.Web.Models.Fileter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FSM.Web.Controllers.BaseInfo
{
    [Authorize]
    public class SysUserController : BaseController
    {
        #region 模块

        private readonly DataResultModel drm;

        private readonly ISysUserBll sysUserBll;

        private readonly IRelUserRoleBll relUserRoleBll;

        #endregion

        #region 构造函数

        public SysUserController(ISysUserBll bll, IRelUserRoleBll relUserRoleBll)
        {
            sysUserBll = bll;
            this.relUserRoleBll = relUserRoleBll;
            drm = new DataResultModel();
            ModuleNo = "0101";
        }

        #endregion

        #region 功能

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0101", "Query")]
        public JsonResult Get(string id)
        {
            sys_user model = sysUserBll.Get(id);
            drm.success = true;
            drm.data = model;
            return drm.ToJson();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="user_no">用户账号</param>
        /// <param name="user_name">用户姓名</param>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0101", "Query")]
        public JsonResult List(string user_no, string user_name, int? page = 1, int? page_size = 30)
        {
            Tuple<List<sys_user>, int> result = sysUserBll.List(user_no, user_name, page.Value, page_size.Value);
            drm.success = true;
            drm.data = result.Item1;
            drm.total = result.Item2;
            return new JsonResult(drm);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0101", "Add")]
        public JsonResult Insert(sys_user model)
        {
            model.creater = AuthencationHelper.GetLoginUserNo(User);
            bool success = sysUserBll.Insert(model);
            drm.success = success;
            drm.data = model;
            return new JsonResult(drm);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0101", "Edit")]
        public JsonResult Update(sys_user model)
        {
            model.editer = AuthencationHelper.GetLoginUserNo(User);
            bool success = sysUserBll.Update(model);
            drm.success = success;
            drm.data = model;
            return new JsonResult(drm);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="user_no">用户账号</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0101", "Delete")]
        public JsonResult Delete(string user_nos)
        {
            string userNo = AuthencationHelper.GetLoginUserNo(User);
            bool success = sysUserBll.Delete(userNo, user_nos);
            drm.success = success;
            return new JsonResult(drm);
        }

        /// <summary>
        /// 还原密码
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        public JsonResult ResetPassword(string id)
        {
            string userNo = AuthencationHelper.GetLoginUserNo(User);
            bool success = sysUserBll.ResetPassword(userNo, id);
            drm.success = success;
            return drm.ToJson();
        }

        #endregion

        #region 角色

        /// <summary>
        /// 获得用户拥有的角色信息
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0101", "Power")]
        public JsonResult Roles(string id)
        {
            List<rel_user_role> result = relUserRoleBll.List(id, "");
            drm.success = true;
            drm.data = result.Select(p => p.role_no);
            return drm.ToJson();
        }

        /// <summary>
        /// 赋于角色对应的菜单信息
        /// </summary>
        /// <param name="id">角色编号</param>
        /// <param name="menu_nos">菜单编号</param>
        /// <returns></returns>
        [HttpPost]
        [ResultExceptionFilter]
        [OperateAuthorize("0101", "Power")]
        public JsonResult GrantRoles(string id, string role_nos)
        {
            string userNo = AuthencationHelper.GetLoginUserNo(User);
            drm.success = relUserRoleBll.BatchInsert(userNo, id, (role_nos ?? "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList());
            if (!drm.success)
            {
                throw new Exception("操作失败");
            }
            return drm.ToJson();
        }

        #endregion
    }
}
